<template>
  <Modal
    v-model="show"
    title="修改密码"
    :mask-closable="false"
    width="500px"
    @on-cancel="closeDialog">
    <Form ref="password" :model="formInfo" :label-width="100" label-position="right" :rules="passwordRule">
      <FormItem label="新密码" prop="newPass">
        <Input v-model="formInfo.newPass" :type="showNew ? 'password' : ''" placeholder="请输入新密码">
          <template slot="append">
            <img v-show="!showNew" class="pass-img" src="../../../../assets/images/passclose.png" @click="showNew = true" />
            <img v-show="showNew" class="pass-img" src="../../../../assets/images/passopen.png" @click="showNew = false" />
          </template>
        </Input>
      </FormItem>
      <FormItem label="确认新密码" prop="rePass">
        <Input v-model="formInfo.rePass" :type="showRePass ? 'password' : ''" placeholder="请输入确认新密码">
          <template slot="append">
            <img v-show="!showRePass" class="pass-img" src="../../../../assets/images/passclose.png" @click="showRePass = true" />
            <img v-show="showRePass" class="pass-img" src="../../../../assets/images/passopen.png" @click="showRePass = false" />
          </template>
        </Input>
      </FormItem>
    </Form>
    <template #footer>
      <Button type="warning" @click="closeDialog">取消</Button>
      <Button :loading="confirmLoading" type="primary" @click="confirmDialog">确认</Button>
    </template>
  </Modal>
</template>

<script>
import md5 from 'js-md5';
import loginApi from '@/api/login.js';

const setNewPass = (rule, value, callback) => {
  const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)/;
  if (!regex.test(value)) {
    callback(new Error('必须包含大写字母、小写字母和数字'));
  } else {
    callback();
  }
};
export default {
  name: "UpdatePassword",
  data() {
    return {
      show: false,
      formInfo: {},
      confirmLoading: false,
      showNew: true,
      showRePass: true,
      passwordRule: {
        newPass: [
          {type: 'string', required: true, message: '请输入新密码', trigger: 'blur'},
          {min: 8, message: '请至少输入8个字符', trigger: 'blur'},
          {validator: setNewPass, trigger: 'blur'},
          {max: 32, message: '最多输入32个字符', trigger: 'blur'}
        ],
        rePass: [
          {type: 'string', required: true, message: '请再次输入新密码', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    closeDialog() {
      this.show = false;
      this.formInfo = {};
    },
    open() {
      this.show = true;
    },
    confirmDialog() {
      this.$refs.password.validate(valid => {
        const {newPass, rePass} = this.formInfo;
        if (valid) {
          if (newPass !== rePass) {
            return this.$Message.warning('两次密码不匹配！');
          }
          this.confirmLoading = true;
          let newPassword = md5(this.formInfo.newPass).toUpperCase();
          loginApi.modifyMyPassword({
            newPassword
          }).then(res => {
            const response = res.data;
            this.confirmLoading = false;
            if (response.returnCode === 200) {
              this.closeDialog();
              this.$Message.success('更新成功');
            } else {
              this.$Message.warning(response.returnMsg);
            }
          }).catch(err => {
            this.confirmLoading = false;
            this.$Message.warning('网络错误！');
          });
        }
      });
    }
  }
}
</script>

<style scoped lang="less">
.pass-img {
  width: 20px;
  height: 20px;
}
</style>
